<template>
  <div class="bar">
    <div class="left" style="padding:0 20px">
      <span style="display:flex">
        <img style="width:30px;height:30px" src="@/assets/logo.png" alt="">
      </span>
      <span class="name">鱼塘CRM后台管理系统</span>
    </div>
    <div class="right">
      <!-- <div class="el-icon-bell msg" style="cursor:pointer"></div> -->
      <div>
        <!-- <img :src="user.avatarUrl" alt=""> -->
        <span v-if="user">您好，{{user.userName}} ！</span>
      </div>
      <div @click="pwdFlag = true" style="cursor:pointer;margin-left:40px">
        修改密码
      </div>
      <!-- <div @click="nickNameFlag = true" style="cursor:pointer;">
        修改昵称
      </div> -->
      <div @click="out" style="cursor:pointer">
        退出登录
      </div>
    </div>
    <el-dialog title="修改密码" :visible.sync="pwdFlag">
      <el-form :model="form" ref="form" label-width="80px" :inline="false" size="mini">
        <el-form-item label="原密码">
          <el-input v-model="form.oldPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="form.newPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input v-model="form.confirmPassword" type="password"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="pwdFlag = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">确认修改</el-button>
      </span>
    </el-dialog>

    <el-dialog title="修改昵称" :visible.sync="nickNameFlag">
      <el-form :model="form2" label-width="80px" :inline="false" size="mini">
        <el-form-item label="昵称">
          <el-input v-model="form2.userName" placeholder="请输入要修改的昵称"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="nickNameFlag = false">取消</el-button>
        <el-button type="primary" @click="onSubmit2">确认修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'TopBar',
  data() {
    return {
      keyWord: '',
      clearArr: ['isLogin', 'meunListSelectIndex'],
      user: "",
      pwdFlag: false,
      form: {
        "oldPassword": "",
        "newPassword": "",
        "confirmPassword": ""
      },
      nickNameFlag: false,
      form2: {
        userName: ""
      }
    }
  },
  created() {
    this.$api.getcurraccountinfo().then(res => {
      this.user = res.data
    })
  },
  mounted() {
    this.$bus.$on("getUserAgain", res => {
      this.$api2.userinfo().then(res => {
        this.user = res.data
      })
    })
  },
  methods: {

    onSubmit2() {
      if (this.form.userName == '') {
        return this.$showTips("原密码必填", "error")
      }
      this.form2.id = this.user.id
      this.$api.updateMsg(this.form2).then(res => {
        this.$myCallBack(res, '修改成功', () => {
          this.nickNameFlag = false
          // this.$router.push("/login") 
        })
      })
    },
    onSubmit() {
      if (this.form.oldPassword == '') {
        return this.$showTips("原密码必填", "error")
      }
      if (this.form.newPassword == '') {
        return this.$showTips("新密码必填", "error")
      }
      if (this.form.confirmPassword == '') {
        return this.$showTips("确认密码必填", "error")
      }
      if (this.form.newPassword != this.form.confirmPassword) {
        return this.$showTips("两次密码不一致", "error")
      }
      this.$api.updatepassword(this.form).then(res => {
        this.$myCallBack(res, '修改成功', () => {
          this.pwdFlag = false
          this.$router.push("/login")
        })
      })
    },
    out() {
      this.$confirm('是否退出登录', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '已退出!'
        })
        window.localStorage.clear()
        this.$router.replace('/login')
      })
    }
  }
}
</script>

<style lang="scss" >
.bar {
  height: 60px;
  display: flex;
  justify-content: space-between;
  color: #fff;
  align-items: center;
  min-width: 1024px;
  background: #fff;
  border-bottom: 1px solid #ebebeb;
  box-sizing: border-box;
  .left {
    display: flex;
    // border: 1px solid;
    align-items: center;
    .name {
      color: #999;
      font-size: 14px;
      margin-left: 10px;
    }
  }
  .right {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-right: 50px;
    color: #666;
    font-size: 13px;
    div {
      margin-right: 20px;
    }
    .msg {
      font-size: 30px;
      margin-left: px;
    }
    img {
      height: 46px;
      width: 46px;
      border-radius: 50%;
      vertical-align: middle;
      margin: 0 15px;
    }
    .login-out {
      font-size: 26px;
      margin-left: 50px;
    }
  }
}
</style>